/**
 * Core types for ComfyUI SDK
 */

// Base ComfyUI types
export interface ComfyUINode {
  class_type: string;
  inputs: Record<string, unknown>;
  _meta?: {
    title?: string;
  };
}

export interface ComfyUIWorkflow {
  [nodeId: string]: ComfyUINode;
}

// Template system types
export interface ParameterSchema {
  type: 'string' | 'number' | 'boolean' | 'array' | 'object';
  required?: boolean;
  default?: unknown;
  description?: string;
  enum?: unknown[];
  min?: number;
  max?: number;
  pattern?: string;
  items?: ParameterSchema;
  properties?: Record<string, ParameterSchema>;
}

export interface TemplateMetadata {
  id: string;
  name: string;
  description?: string;
  version?: string;
  author?: string;
  tags?: string[];
  category?: string;
  createdAt?: Date;
  updatedAt?: Date;
}

export interface WorkflowTemplateData {
  metadata: TemplateMetadata;
  workflow: ComfyUIWorkflow;
  parameters: Record<string, ParameterSchema>;
}

// Client configuration types
export interface ComfyUIClientConfig {
  baseUrl: string;
  timeout?: number;
  retryAttempts?: number;
  retryDelay?: number;
  headers?: Record<string, string>;
}

// Event types
export interface ExecutionProgress {
  nodeId: string;
  progress: number;
  max: number;
  timestamp: Date;
}

export interface ExecutionResult {
  promptId: string;
  outputs: Record<string, unknown>;
  executionTime: number;
  timestamp: Date;
}

export interface ExecutionError {
  nodeId?: string;
  message: string;
  details?: unknown;
  timestamp: Date;
}

// Callback types
export interface ExecutionCallbacks {
  onProgress?: (progress: ExecutionProgress) => void;
  onExecuting?: (nodeId: string) => void;
  onExecuted?: (result: ExecutionResult) => void;
  onError?: (error: ExecutionError) => void;
}

// WebSocket message types
export interface WSMessage {
  type: string;
  data: unknown;
}

export interface WSProgressMessage extends WSMessage {
  type: 'progress';
  data: {
    value: number;
    max: number;
    node?: string;
  };
}

export interface WSExecutingMessage extends WSMessage {
  type: 'executing';
  data: {
    node: string | null;
    prompt_id: string;
  };
}

export interface WSExecutedMessage extends WSMessage {
  type: 'executed';
  data: {
    node: string;
    output: Record<string, unknown>;
    prompt_id: string;
  };
}

// Template execution types
export interface ExecutionOptions {
  callbacks?: ExecutionCallbacks;
  timeout?: number;
  priority?: number;
}

export interface TemplateExecutionResult {
  promptId: string;
  success: boolean;
  outputs?: Record<string, unknown>;
  error?: ExecutionError;
  executionTime: number;
}

// Utility types
export type ParameterValues = Record<string, unknown>;
export type VariableSyntax = '{{}}' | '${}' | '@{}';

// Template validation types
export interface ValidationResult {
  valid: boolean;
  errors: ValidationError[];
}

export interface ValidationError {
  path: string;
  message: string;
  value?: unknown;
}

// Export all types
export * from './events';
export * from './api';
